package com.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.dao.CreditDao;
import com.dto.Apply;
import com.dto.Fenqi;
import com.dto.Retmsg;
import com.dto.UserInfo;
import com.service.CreditService;
import com.util.FinancialComputing;
@Service
public class CreditServiceImpl implements CreditService  {
	@Autowired
	private CreditDao cd;
	

	
	@Override
	@Cacheable(value="RetmsgInfo")
	public List<Retmsg> queryRetmsgInfo(String u_id) {
		System.err.println("---肥超");
		return cd.queryRetmsgInfo(u_id);
	}
	
	
	/**@param
	 * @return
	 * @用途:还款
	 */
	@Transactional
	@Override
	public boolean rembursementById(String ret_type){
		int x=0;
		try {
			UserInfo user=(UserInfo) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("userInfo");
			/*UserInfo user=new UserInfo();
			user.setU_id("3");*/
			Retmsg retmsg=(Retmsg) cd.queryNperById(user.getU_id());//查询到用户的贷款详情
			if(retmsg==null) {//第一次还款
				retmsg=new Retmsg();
			}
			if(retmsg.getNper()==null) {
				retmsg.setNper(0);
			}
			retmsg.setUser_id(user.getU_id());//用户id
			retmsg.setNper(retmsg.getNper()+1);//最新期数
			retmsg.setRet_water(com.util.Serialnumber.Getnum());//还款流水
			retmsg.setRet_type(ret_type);//贷款类型
			retmsg.setRet_num(cd.queryPay_Num(user.getU_id()));//登录人贷款编号
			retmsg.setRet_date(cd.queryEdate(user.getU_id()));
			if(retmsg.getRet_date()==null) {//第一次还款
				Date date=new Date();	
				retmsg.setRet_date(date);
				
			}
			Double d=(double) cd.queryMoney(user.getU_id());//转化现金
			/**活期数*/
			List<Fenqi> fq=cd.queryfenqqqishu();
			int qishu=0;
			for (Fenqi fenqi : fq) {
				if(ret_type.equals(fenqi.getFid())) {
					qishu=fenqi.getQishu();
				}
			}
				if(ret_type.equals("101") || ret_type.equals("102") ||ret_type.equals("103") ||ret_type.equals("104") || ret_type.equals("105")) {	
					d=FinancialComputing.averageCapitalPlusInterest(d, qishu);//等额等息 记得修改期数
					retmsg.setRet_money(d);//存放
					x=cd.repaymentOperation(retmsg);
				}
				if("201".equals(ret_type) || "202".equals(ret_type) ||"203".equals(ret_type) ||"204".equals(ret_type)) {
					d=FinancialComputing.xiBen(d, retmsg.getNper()+1, qishu, ret_type);//月数也就是期数
					retmsg.setRet_money(d);//存放
					x=cd.repaymentOperation(retmsg);
				}
		}catch(Exception e){
			e.printStackTrace();
		}
		if(x>0) {
			return true;
		}else {
			return false;
		}
	}


	/**查询还款时间*/
	@Override
	public Date queryEdate(String u_id) {		
		return cd.queryEdate(u_id);
	}


	/**偿还贷款查询*/
	@Override
	public Double queryRepaymentAmount(String ret_type, String ret_num,String u_id) {
		List<Fenqi> fq=cd.queryfenqqqishu();
		int qishu=0;
		for (Fenqi fenqi : fq) {
			if(ret_type.equals(fenqi.getFid())) {
				qishu=fenqi.getQishu();
			}
		}
		Double d=(double) cd.queryMoney(u_id);//转化现金
		if(ret_type.equals("101") || ret_type.equals("102") ||ret_type.equals("103") ||ret_type.equals("104") || ret_type.equals("105")) {	
			d=FinancialComputing.averageCapitalPlusInterest(d, qishu);//等额等息 记得修改期数
			
		}
		if("201".equals(ret_type) || "202".equals(ret_type) ||"203".equals(ret_type) ||"204".equals(ret_type)) {
			Retmsg ret=cd.queryNperById(u_id);//期数
			if(ret==null) {
				ret=new Retmsg();
				ret.setNper(0);//具体的期数
			}
			d=FinancialComputing.xiBen(d, ret.getNper(), qishu, ret_type);//月数也就是期数--查询具体类型对应的期数
		}
		return d;
	}
	
	
	

	@Override
	public Retmsg queryNperById(String u_id) {
		return cd.queryNperById(u_id);
	}


	@Override
	public Apply queryApply(String u_id) {
		return cd.queryApply(u_id);
	}


	@Override
	public Double queryMoney(String u_id) {
		return cd.queryMoney(u_id);
	}


	
	/**查询分期的集合*/
	@Override
	public List<Fenqi> queryfenqqqishu() {
		
		return cd.queryfenqqqishu();
	}

	/**@param
	 * @return
	 * @用途:还款根据id修改余额
	 */
	@Override
	public int updateBalanceById(UserInfo user) {
		int i=cd.updateBalanceById(user);
		return i;
	}


	@Override
	public Apply queryApplyById(String u_id) {
		return cd.queryApplyById(u_id);
	}
	
	
}
